package com.syq.play.ui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import com.syq.play.AppContent;
import com.syq.play.R;
import com.syq.play.adapter.FolderGridAdapter;
import com.syq.play.adapter.IndexAdapter;
import com.syq.play.adapter.MediaListAdapter;
import com.syq.play.api.MediaStoreApi;
import com.syq.play.db.entity.SongManager;
import com.syq.play.impl.MediaStoreImpl;
import com.syq.play.model.MediaModel;
import com.syq.play.service.LocalService;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MediaActivity extends Activity implements OnClickListener, OnItemClickListener{

	private static final String TAG="ListViewActivity";
	/**艺术家*/
	public static final int TYPE_ALL=0;
	/**我的最爱*/
	public static final int TYPE_FAVORITE=1;
	/**文件夹*/
	public static final int TYPE_RECENT=2;
	/**艺术家*/
	public static final int TYPE_ARTIST=3;
	/**专辑*/
	public static final int TYPE_ALBUM=4;
	/**文件夹*/
	public static final int TYPE_FOLDER=5;
	
	private Context context;
	private LinearLayout llBack;
	private TextView tvTitle;
	private ListView listView;
	private MediaListAdapter mediaAdapter;
	private PrevNextReceiver prevNextReceiver;
	private String title;
	private int type=-1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_listview);
		context=this;
		initData();
		initUi();
		initListener();
		getMediaData();
	}
	private void initData(){
		Intent intent=getIntent();
		Bundle data=intent.getExtras();
		if(data!=null && !data.isEmpty()){
			title=data.getString(IndexActivity.KEY_TITLE);
			type=data.getInt(IndexActivity.KEY_TYPE);
		}
	}
	private void initUi(){
		llBack=(LinearLayout)findViewById(R.id.ll_left);
		tvTitle=(TextView)findViewById(R.id.tv_title);
		listView=(ListView)findViewById(R.id.listview);
		tvTitle.setText(title);
		
	}
	private void initListener(){
		llBack.setOnClickListener(this);
		listView.setOnItemClickListener(this);
	}
	/**
	 * 显示音频文件方法
	 */
	private void getMediaData(){
		new AsyncTask<Void, Void, List<MediaModel>>() {

			@Override
			protected List<MediaModel> doInBackground(Void... params) {
				List<MediaModel> result=null;
				switch(type){
				case TYPE_ALL:
					result=AppContent.mSong.getAllSong();
					break;
				case TYPE_FAVORITE:
					result=AppContent.mSong.getAllFavoriteSong();
					break;
				case TYPE_RECENT:
					result=AppContent.mSong.getAllRecentSong();
					break;
				case TYPE_ALBUM:
					result=AppContent.mSong.getAllSongFromAlbum(title);
					break;
				case TYPE_ARTIST:
					result=AppContent.mSong.getAllSongFromArtist(title);
					break;
				case TYPE_FOLDER:
					result=AppContent.mSong.getAllSongFromFolder(title);
					break;
				}
				return result;
			}
			protected void onPostExecute(List<MediaModel> result) {
				if(result!=null && !result.isEmpty()){
					if(type==TYPE_FAVORITE){
						mediaAdapter=new MediaListAdapter(context, result
								, MediaListAdapter.TYPE_PLAY_COUNT);
					}else if(type==TYPE_RECENT){
						mediaAdapter=new MediaListAdapter(context, result
								, MediaListAdapter.TYPE_PLAY_TIME);
					}else{
						mediaAdapter=new MediaListAdapter(context, result);
					}
					listView.setAdapter(mediaAdapter);
				}else{
					mediaAdapter=new MediaListAdapter(context, new ArrayList<MediaModel>());
					listView.setAdapter(mediaAdapter);
				}
			};
		}.execute();
	}
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.ll_left:
			goBack();
			break;
		}
	}
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
			MediaModel item=mediaAdapter.getListMedia().get(position);
			AppContent.playId=item.getMediaId();
			AppContent.listData=mediaAdapter.getListMedia();
			if(item!=null){
				SRPlayActivity.mActivityGroup.playMusic(position);
				SongManager.getInstance().addPlayedSong(item.getMediaData());
				item.setPlayCount(item.getPlayCount()+1);
				item.setPlayTime(Calendar.getInstance().getTimeInMillis());
			}
//			AppContent.playIndex=position;
//			mediaAdapter.notifyDataSetChanged();
	}
	/**
	 * 返回方法
	 */
	private void goBack(){
		Bundle data=new Bundle();
		switch(type){
		case TYPE_ALBUM:
			data.putString(IndexActivity.KEY_TITLE, "专辑");
			data.putInt(IndexActivity.KEY_TYPE, FolderActivity.TYPE_ALBUM);
			SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
			break;
		case TYPE_ARTIST:
			data.putString(IndexActivity.KEY_TITLE, "艺术家");
			data.putInt(IndexActivity.KEY_TYPE, FolderActivity.TYPE_ARTIST);
			SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
			break;
		case TYPE_FOLDER:
			data.putString(IndexActivity.KEY_TITLE, "文件夹");
			data.putInt(IndexActivity.KEY_TYPE, FolderActivity.TYPE_FOLDER);
			SRPlayActivity.mActivityGroup.mStartActivity(FolderActivity.class, data);
			break;
		default:
				SRPlayActivity.mActivityGroup.mStartActivity(IndexActivity.class, null);
		}
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onStart() {
		super.onStart();
		prevNextReceiver=new PrevNextReceiver();
		registerReceiver(prevNextReceiver, new IntentFilter(SRPlayActivity.ACTION_PREV_NEXT));
	}
	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(prevNextReceiver);
	}
	
	class PrevNextReceiver extends BroadcastReceiver{
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if(mediaAdapter!=null){
				Bundle data=intent.getExtras();
				AppContent.playId=data.getInt(LocalService.ID);
				mediaAdapter.notifyDataSetChanged();
			}
		}
	}
}
